package dao

import (
	"context"
	"device-admin/internal/dal"
	"device-admin/internal/models"
	"strings"
	"time"

	"192.168.1.75/go-pkg/errorx"
	"192.168.1.75/go-pkg/helper"
	"gorm.io/gorm"
)

type MaterialFolder struct{}

// 新建分组
func (MaterialFolder) Create(ctx context.Context, db *gorm.DB, merchantID, name string, Type int8) (err error) {
	m := dal.Use(db).MaterialFolder

	var data models.MaterialFolder
	data.ID, _ = helper.UUID{}.String()
	data.MerchantID = merchantID
	data.Name = strings.TrimSpace(name)
	data.Type = Type
	data.CreatedAt = time.Now().Unix()
	m.WithContext(ctx).Create(&data)
	return nil
}

// 更新分组
func (MaterialFolder) Update(ctx context.Context, db *gorm.DB, merchantID, ID, name string, Sort *int8) (err error) {
	m := dal.Use(db).MaterialFolder
	tx := m.WithContext(ctx).Where(m.ID.Eq(ID)).
		Where(m.MerchantID.Eq(merchantID)).
		Where(m.DeletedAt.Eq(0))
	var updateData = models.MaterialFolder{}
	if name != "" {
		updateData.Name = strings.TrimSpace(name)
	}
	if Sort != nil {
		updateData.Sort = *Sort
	}
	tx.UpdateColumns(&updateData)
	return nil
}

// 分组详情
func (MaterialFolder) Detail(ctx context.Context, db *gorm.DB, merchantID, ID string, Type int8) (data *models.MaterialFolder, err error) {
	m := dal.Use(db).MaterialFolder
	tx := m.WithContext(ctx).
		Where(m.ID.Eq(ID)).
		Where(m.MerchantID.Eq(merchantID)).
		Where(m.DeletedAt.Eq(0))
	if Type != 0 {
		tx = tx.Where(m.Type.Eq(Type))
	}
	info, _ := tx.Take()
	return info, nil
}

// 分组列表
func (MaterialFolder) List(ctx context.Context, db *gorm.DB, merchantID string, ID interface{}, Type int8) (data []*models.MaterialFolder, count int64, err error) {
	m := dal.Use(db).MaterialFolder
	tx := m.WithContext(ctx).
		Where(m.MerchantID.Eq(merchantID)).
		Where(m.DeletedAt.Eq(0))
	if ID != nil {
		switch val := ID.(type) {
		case string:
			tx = tx.Where(m.ID.Eq(val))
		case []string:
			tx = tx.Where(m.ID.In(val...))
		}
	}
	if Type != 0 {
		tx = tx.Where(m.Type.Eq(Type))
	}
	count, _ = tx.Count()

	list, err := tx.Order(m.Sort, m.ID).Find()
	return list, count, err
}

// 名称是否占用
func (MaterialFolder) SameNameExist(ctx context.Context, db *gorm.DB, merchantID, name string, Type int8, ExceptID string) (bool, error) {
	m := dal.Use(db).MaterialFolder
	if name == "" {
		return false, errorx.New("名称不能为空", -1)
	}
	tx := m.WithContext(ctx).
		Where(m.MerchantID.Eq(merchantID)).
		Where(m.DeletedAt.Eq(0)).
		Where(m.Name.Eq(name))
	if Type != 0 {
		tx = tx.Where(m.Type.Eq(Type))
	}
	if ExceptID != "" {
		tx = tx.Where(m.ID.Neq(ExceptID))
	}
	count, err := tx.Count()
	return count == 1, err
}

// 删除分组
func (MaterialFolder) Delete(ctx context.Context, db *gorm.DB, merchantID, ID string) (err error) {
	if ID == "" {
		return errorx.New("ID不能为空", -1)
	}
	m := dal.Use(db).MaterialFolder
	m.WithContext(ctx).
		Where(m.MerchantID.Eq(merchantID)).
		Where(m.ID.Eq(ID)).
		UpdateSimple(m.DeletedAt.Value(time.Now().Unix()))
	return
}
